package com.yeban.javase.day20.reflect;

import java.io.InputStream;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/11/4
 */
public class ReflectDemo1 {

    public static void main(String[] args) throws ClassNotFoundException {
        // 1. 通过类名.class获得
        // 使用场景：提前知道类名
        Class<Student> clazz1 = Student.class;
        System.out.println("clazz1 = " + clazz1);
        // 获取流对象
        InputStream is = Student.class.getResourceAsStream("abbc.txt");

        // 2.通过对象名.getClass()方法获得
        // 使用场景：方法中形参类型是Object类型时
        Student stu = new Student();
        Class<? extends Student> clazz2 = stu.getClass();
        System.out.println("clazz2 = " + clazz2);
        System.out.println(clazz1 == clazz2);
        // testobj(stu);

        // 3.Class.forName("包名.类名")
        // 使用场景：使用Spring框架的XML文件中，配置实例bean
        Class<?> clazz3 = Class.forName("com.yeban.javase.day20.reflect.Student");
        System.out.println("clazz3 = " + clazz3);
        System.out.println(clazz3 == clazz2);
    }

    public static void testobj(Object obj) throws InstantiationException, IllegalAccessException {
        Class<?> clazz = obj.getClass();
        System.out.println(clazz);
        Object o = clazz.newInstance();
    }
}
